package fileinput;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.fasterxml.jackson.databind.ObjectMapper;


import bean.ExDomain;
import util.BaseRedisDao;
import util.ConfigUtil;
import util.StringUtil;
public class DeleteRedisData {
	  private static final Log log = LogFactory.getLog(DeleteRedisData.class);
	transient static BaseRedisDao redisDao;
	private String EX_DOMAIN_REDIS_KEY="EXDOMAIN";
	private int exDomainKeepDays = 30;
	public DeleteRedisData(){
		redisDao =new BaseRedisDao();	
	}
	
	public void deleteData(BaseRedisDao redisDao) {
		log.info(" start clean redis Exdomain .........................  ");
		Map<String, String> map = redisDao.getAllExDomainCache(EX_DOMAIN_REDIS_KEY);
		
		if(map!=null && !map.isEmpty()){
			log.info(" start clean redis Exdomain , data size :"+map.size());
			ObjectMapper mapper = new ObjectMapper();
			List<String> delKeys = new ArrayList<String>(1000);
			Map<String, String> updateMap = new HashMap<String, String>(1000);
			Calendar calendar = Calendar.getInstance();
			exDomainKeepDays=Integer.valueOf(ConfigUtil.get("surfilter.massdata.exdomain.redis.keep.days","30")) ;
			calendar.add(Calendar.DAY_OF_YEAR, -1 * exDomainKeepDays);
			Date lastDate = calendar.getTime();//获得前30天的时间转换为HHmmssSSS的时间格式
			ExDomain bean = null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String dateStr = null;
			Date findDate = null;
			for (Entry<String, String> entry : map.entrySet()) {
				try {
					bean = mapper.readValue(StringUtil.toLowerJson(entry.getValue()), ExDomain.class);
					if (bean != null) {
			
						
						dateStr = bean.getFindtime();
						
						
						if (dateStr != null) {
							findDate = sdf.parse(dateStr);
							// 判断泛域名的更新时间是否超过泛域名存储的期限
							if (findDate.before(lastDate)) {
								delKeys.add(entry.getKey());
							}
						} else {
							bean.setFindtime(sdf.format(new Date()));
							updateMap.put(entry.getKey(), mapper.writeValueAsString(bean));
						}
					}
				} catch (Exception e) {
					log.error(" transform str to bean error ");
					e.printStackTrace();
				}

				if (delKeys.size() == 1000) {
					redisDao.hDelExDomains(EX_DOMAIN_REDIS_KEY, delKeys);
					delKeys.clear();
				}

				if (updateMap.size() == 1000) {
					redisDao.hsetExDomains(EX_DOMAIN_REDIS_KEY, updateMap);
					updateMap.clear();
				}
			}
			
			if(!delKeys.isEmpty()){
				redisDao.hDelExDomains(EX_DOMAIN_REDIS_KEY, delKeys);
			}
			
			if(!updateMap.isEmpty()){
				redisDao.hsetExDomains(EX_DOMAIN_REDIS_KEY, updateMap);
			}
		}else{
			log.error("redis exDomain is empty ...........");
		}
	}

	public static void main(String[] args) throws ParseException
	{
		DeleteRedisData deleteData=new DeleteRedisData();
		deleteData.deleteData(redisDao);
		
	}
}
